package com.yiyi.credit.manage.sso.manager.controller;

import com.google.common.collect.ImmutableMap;

import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.entity.SysUser;
import com.yiyi.credit.manage.sso.model.entity.SysUserInfo;
import com.yiyi.credit.manage.sso.service.inner.ShopService;
import com.yiyi.credit.manage.sso.service.inner.SysUserInfoService;
import com.yiyi.credit.manage.sso.service.inner.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserInfoService sysUserInfoService;
    @Autowired
    ShopService shopService;
    private static Logger logger = LoggerFactory.getLogger(SysUserController.class);


    @RequestMapping(value = "/getSysUserPageList")
    public Object getSysUserPageList(String userName, Integer pageIndex, Integer pageSize) throws Exception {

        Pagination pagination = new Pagination(pageIndex, pageSize);
        Pagination result = sysUserService.getSysUserPageList(userName, pagination);
        return new Response<>(result);
    }


    @RequestMapping(value = "/getSysUserById")
    public Object getSysUserById(@RequestParam String sysUserId) throws Exception {
        if (sysUserId == null) {
            return new Response<>(ResponseCode.SUCCESS);
        }
        SysUser sysUser = sysUserService.getSysUserById(Long.parseLong(sysUserId));
        List<Map<Integer,String>> shopList = shopService.getAllShop();
        return new Response<>(ImmutableMap.of("sysUser",sysUser,"shopList",shopList));
    }


    @RequestMapping(value = "/addSysUser")
    public Object register(@RequestBody SysUser sysUser) throws Exception {

        if (sysUser == null) {
            return new Response<>(ResponseCode.NO_SYS_USER);
        }
        if (StringUtils.isEmpty(sysUser.getUserName())) {
            return new Response<>(ResponseCode.USERNAME_NOTNULL);
        }
        sysUserService.register(sysUser.getUserName(), sysUser.getPassword(), sysUser.getStatus(), new SysUserInfo());
        return new Response<>();
    }



    @RequestMapping(value = "/updateSysUser")
    public Object updateSysUser(@RequestBody SysUser sysUser) throws Exception {

        if (sysUser == null) {
            return new Response<>(ResponseCode.NO_SYS_USER);
        }
        sysUser.setPassword(null);
        sysUserService.updateSysUser(sysUser);
        return new Response<>();
    }



    @RequestMapping(value = "/updatePassword")
    public Object updatePassword(@RequestParam Long sysUserId,
                                 @RequestParam String oldPassword,
                                 @RequestParam String newPassword) throws Exception {

        sysUserService.updatePassword(sysUserId, oldPassword, newPassword);
        return new Response<>();
    }



    @RequestMapping(value = "/deleteSysUserRole")
    public Object deleteSysUserRole(@RequestBody SysUser sysUser) throws Exception {

        sysUserService.deleteSysUserRole(sysUser);
        return new Response<>();
    }



    @RequestMapping(value = "/deleteSysUser")
    public Object deleteSysUser(@RequestBody SysUser sysUser) throws Exception {
        sysUserService.deleteSysUser(sysUser);
        return new Response<>();
    }



    @RequestMapping(value = "/getUserAllInfo")
    public Object getUserAllInfo(String sysUserId) throws Exception {

        return new Response<>();
    }


    //
    // @RequestMapping(value = "/getUserAllInfoList")
    // public Object getUserAllInfoList(String sysInfoId,Integer pageIndex, Integer pageSize) throws Exception {
    //     Pagination pagination = new Pagination(pageIndex, pageSize);
    //     return new Response<>(sysUserService.getUserAllInfoList(sysInfoId,pagination));
    // }

}



